<template>
    <div v-if="remote" :style="remoteStyle" class="remote-icon svg-icon" v-on="$listeners" />
    <svg v-else :class="svgClass" :style="fillStyle" aria-hidden="true" v-on="$listeners">
        <use :href="iconName" />
    </svg>
</template>

<script>
export default {
    name: 'IconSvg',
    props: {
        iconClass: {
            type: String,
            required: true,
        },
        className: {
            type: String,
            default: '',
        },
        fill: {
            type: String,
            default: 'currentColor',
        },
    },
    computed: {
        remote() {
            const clazz = this.iconClass
            return clazz.includes('http') || clazz.includes('https')
        },
        iconName() {
            return `#icon-${this.iconClass}`
        },
        svgClass() {
            if (this.className) {
                return 'svg-icon ' + this.className
            } else {
                return 'svg-icon'
            }
        },
        fillStyle() {
            return {
                fill: this.fill,
            }
        },
        remoteStyle() {
            const url = this.iconClass
            return {
                mask: `url(${url}) no-repeat 50% 50%`,
                '-webkit-mask': `url(${url}) no-repeat 50% 50%`,
            }
        },
    },
}
</script>

<style scoped>
.svg-icon {
    width: 1em;
    height: 1em;
    vertical-align: -0.15em;
    overflow: hidden;
}

.remote-icon {
    background-color: currentColor;
    mask-size: cover !important;
    display: inline-block;
}
</style>
